package org.example.cloudcommon.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;

@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig implements AsyncConfigurer {

  /*  @Autowired
13    private TaskExecutor taskExecutor;

*/
    @Value("${executor.setCorePoolSize}")
    private int setCorePoolSize;
    @Value("${executor.setMaxPoolSize}")
    private int setMaxPoolSize;
    @Value("${executor.setQueueCapacity}")
    private int setQueueCapacity;
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(setCorePoolSize); // 核心线程池
        executor.setMaxPoolSize(setMaxPoolSize); // 最大线程池数量
        executor.setQueueCapacity(setQueueCapacity); // 队列容量
        executor.setThreadNamePrefix("this-excutor-");  // 线程名称的前缀
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
       /* return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();*/
        return new CustomAsyncExceptionHandler();
    }
}

class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler{

    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        System.out.println("异常处理");
    }
}
